package com.sphd.beautysalon.fragments;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.beautysalon.adapters.VipAdapter;
import com.sphd.beautysalon.interfaces.VipNoCallBack;
import com.sphd.beautysalon.view.VipView;
import com.sphd.beautysalon.R;
import com.sphd.beautysalon.activitys.vip.VipInfomationActivity;
import com.sphd.beautysalon.base.LazyLoadFragment;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.info.VipInfo;
import com.sphd.beautysalon.presenter.VipPresenterImpl;
import com.sphd.beautysalon.utils.Constant;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Hu on 2016/11/10.
 * 折扣会员
 */

public class DisFragment extends LazyLoadFragment implements VipView, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    VipPresenterImpl vipPresenter;
    private int curpage = 1;
    int TYPE = 0;
    int id;
    VipNoCallBack vipCallBack;
    VipAdapter vipAdapter;
    private static final String TAG = "DisFragment";
    RestaurantInfo restaurantInfo;
    Intent intent;
    Bundle bundle;

    public void setVipCallBack(VipNoCallBack vipCallBack) {
        this.vipCallBack = vipCallBack;
    }

    @Override
    public int getLayout() {
        return R.layout.swf_and_recy;
    }

    @Override
    public void initViews(View rootView) {
        ButterKnife.bind(this, rootView);
        intent = new Intent();
        bundle = new Bundle();
        setRefresh(mySwf);
        setRecy(myRecy);
        vipAdapter = new VipAdapter(R.layout.item_vip, new ArrayList<>());
        myRecy.setAdapter(vipAdapter);
        vipAdapter.openLoadAnimation();
        vipAdapter.openLoadMore(10);
        vipPresenter = new VipPresenterImpl(this);
        initEvent();
    }

    private void initEvent() {
        vipAdapter.setOnLoadMoreListener(() -> loadData());
        mySwf.setOnRefreshListener(this);
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                bundle.putSerializable(Constant.RESTAURANTSINFO, restaurantInfo);
                bundle.putSerializable(Constant.VIPINFO, (Serializable) baseQuickAdapter.getItem(i));
                intent.putExtras(bundle);
                intent.setClass(context, VipInfomationActivity.class);
                startActivityForResult(intent, 1);
            }
            @Override
            public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemLongClick(adapter, view, position);
                showDeleVipDialog(position);

            }
        });
    }
    /**
     * 弹出来删除会员的提示框
     */
    private void showDeleVipDialog(final int positon) {
        VipInfo vipInfo = vipAdapter.getItem(positon);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
        // set dialog message
        alertDialogBuilder
                .setTitle("删除提示")
                .setMessage("你将删除" + ((VipInfo) (vipAdapter.getItem(positon))).getMem_name() + "会员?")
                .setCancelable(false)
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                vipPresenter.delUrl(RestaurantUrl.Httptitle + RestaurantUrl.DelVip
                                        + "id=" + vipInfo.getId());
                            }
                        })
                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();

    }
    @Override
    public void loadData() {
        vipPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.ShowVip + "type=" + TYPE + "&cgid=" + id + "&grade=1" + "&curpage=" +
                curpage + "&pageno=10");
    }

    public void setVal(int type, RestaurantInfo restaurantInfo) {
        this.TYPE = type;
        this.restaurantInfo = restaurantInfo;
        this.id = restaurantInfo.getId();
    }

    @Override
    public void recRes(int com, int dis, int topup,int topupno) {
        vipCallBack.recRes(com, dis, topup,topupno);
    }

    @Override
    public void recList(List<VipInfo> vipInfos) {
        Log.e(TAG, vipInfos.size() + "");

        setLoadDataCompleted(true);
        if (curpage == 1) {
            vipAdapter.setNewData(vipInfos);
        } else {
            vipAdapter.addData(vipInfos);
        }
        if (vipInfos.size() != 10) {
            vipAdapter.loadComplete();
            addFootView(vipAdapter);
        } else {
            curpage++;
        }
    }

    @Override
    public void recDelRes(int res) {

        curpage = 1;
        loadData();
    }

    public void changeType() {
        curpage = 1;
        setLoadDataCompleted(false);
    }

    @Override
    public void onRefresh() {
        curpage = 1;
        loadData();
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
        mySwf.setRefreshing(false);
    }
}
